package Review.ArrayList;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class playCards {
    private  final String[] suit = {"♣","♥","♦","♠"};
    private List<playingCard> Card = new ArrayList<>();
    //获取花色
    public String get(int data){
        return suit[data];
    }
    public void buyCard(){
        for (int i = 0; i <=13; i++) {
            for (int j=0;j<4;j++){
                String  tmp =  get(j);//获取j位置的花色
                playingCard playingCard = new playingCard(tmp,i);//花色和点数
                this.Card.add(playingCard);//增加到牌中
            }
        }
        this.Card.add(new playingCard("大王",0));//最后放入大小王
       this. Card.add(new playingCard("小王",0));
    }
    private static  void swap(List<playingCard> Card,int i,int j){
       playingCard playingCard = Card.get(i);
       Card.set(i,Card.get(j));
        Card.set(j,playingCard);
    }
    public  void shuffleCard(){
        Random random = new Random();
        int countCard = this.Card.size();//当前扑克牌的数量
        //从后往前产生随机数打乱牌
        for (int i = countCard-1; i >0 ; i--) {
            int tmpCard = random.nextInt(i);
            //交换
            swap(this.Card,i,tmpCard);
        }
    }
    public  void touchCard(String name1,String name2,String name3){
        List<List<playingCard>> list = new ArrayList<>();
        List<playingCard> user1 = new ArrayList<>();
        List<playingCard> user2 = new ArrayList<>();
        List<playingCard> user3 = new ArrayList<>();
        //把三个用户放入顺序表中
        list.add(user1);
        list.add(user2);
        list.add(user3);
        //给每个用户发牌
        for (int i = 0; i < 18; i++) {
            for (int j =0;j<3;j++){
                List<playingCard> tmp = list.get(j);//获取顺序表指定的对象
                tmp.add(Card.remove(0));//删除第一个元素并放入指定对象中
            }
        }
        //打印每个用户摸到的扑克牌
        String [] name = {name1,name2,name3};
        for (int i = 0; i < list.size(); i++) {
            System.out.print(name[i]+" ");
            System.out.println(list.get(i));
        }
    }
    public static void main(String[] args) {
        playCards playCards = new playCards();
        playCards.buyCard();//卖一副牌
        System.out.println("买到的新牌"+playCards);
        playCards.shuffleCard();//洗牌
        System.out.println("洗完的牌 "+playCards);
        Scanner scanner = new Scanner(System.in);
        String[] user = new String[3];
        System.out.println("请输入打牌的三个人： ");
        for (int i = 0; i < user.length; i++) {
            user[i] = scanner.nextLine();
        }
        playCards.touchCard(user[0],user[1],user[2] );//对应三个人摸牌

    }
}
